package com.open.capacity.user.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.prints.util.uuid.UUIDUtils;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.user.dao.SysPermissionDao;
import com.open.capacity.user.model.SysPermission;
import com.open.capacity.user.service.SysPermissionService;
import com.open.capacity.user.service.SysUserService;
import com.open.capacity.user.vo.SysPermissionVo;
import com.open.capacity.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 作者 ftquan E-mail: 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 */
@Slf4j
@Service
public class SysPermissionServiceImpl implements SysPermissionService {
    @Autowired
    private SysPermissionDao authorizeDao;

    @Autowired
    private SysUserService userService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysPermission sysAuthorize) {
        sysAuthorize.setCreateTime(new Date());

        authorizeDao.save(sysAuthorize);
        log.info("保存数据授权：{}", sysAuthorize);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByObjectId(String objectId) {

        authorizeDao.deleteByObjectId(objectId);

        log.info("删除数据授权：{}", objectId);
    }


    @Override
    public List<SysPermission> findByObject(String objectType, String objectId) {
        List<SysPermission> list = authorizeDao.findByObject(objectType, objectId);
        if (CollectionUtils.isEmpty(list)) {
            SysUser user = userService.findById(objectId);
            if (user != null && StringUtil.isNotEmpty(user.getRoleIds())) {
                for (String roleId : user.getRoleIds().split(",")) {
                    List<SysPermission> rolePermissions = authorizeDao.findByObject("Role", roleId);
                    for (SysPermission permission : rolePermissions) {
                        if (!list.contains(permission)) {
                            list.add(permission);
                        }
                    }
                }
            }
        }
        return list;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveOrUpdate(SysPermissionVo sysAuthorize) {
        int i = 0;
        authorizeDao.deleteByObjectId(sysAuthorize.getObjectId());
        Date now = new Date();
        if (!CollectionUtils.isEmpty(sysAuthorize.getItemIds())) {
            sysAuthorize.getItemIds().forEach(b -> {
                SysPermission model = new SysPermission();
                model.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                model.setObjectType(sysAuthorize.getObjectType());
                model.setObjectId(sysAuthorize.getObjectId());
                model.setItemId(b);
                model.setItemType(9);
                model.setCreateTime(now);
                authorizeDao.save(model);
            });
        } else {
            SysPermission model = new SysPermission();
            model.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            model.setObjectType(sysAuthorize.getObjectType());
            model.setObjectId(sysAuthorize.getObjectId());
            model.setItemType(sysAuthorize.getItemType());
            model.setCreateTime(now);
            authorizeDao.save(model);
        }

        return 1 > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }

}
